﻿layui.use(['table', 'laypage', 'form'], function () {
    var table = layui.table;
    var laypage = layui.laypage;
    var layer = layui.layer;
    var $ = layui.$;
    var form = layui.form;

    var pageSize = 15; // 每页显示条数
    //获取列表
    function getListData(pageIndex) {
        //获取查询条件
        var RoleName = $('#RoleName').val();
        //ajax后台交互
        $.ajax({
            url: 'http://10.31.60.18:8000/AuthorizationPeopleGetRout/api/RoleShow/ShowPageRoleInfo',
            type: 'get',
            dataType: 'json',
            //参数
            data: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                RoleName: RoleName,
            },
            success: function (res) {
                //渲染表格数据
                table.render({
                    //挂载的表格id
                    elem: '#ID-table-demo-setRowChecked',
                    cols: [[
                        { type: 'checkbox', width: 60, fixed: 'center', unresize: true, align: 'center' },
                        { type: 'numbers', title: '序号', width: 60, fixed: 'center', unresize: true, align: 'center' },
                        { field: 'roleName', title: '角色名称', width: 120, align: 'center' },
                        { field: 'roleDescription', title: '角色描述', width: 120, align: 'center' },
                        { field: 'createBy', title: '操作人', width: 120, align: 'center' },
                        { field: 'createDate', title: '操作时间', width: 120, align: 'center', templet: function (data) { return data.createDate.substring(0, 10) } },
                        { field: 'updateBy', title: '修改人', width: 120, align: 'center' },
                        { field: 'updateDate', title: '修改时间', width: 120, align: 'center', templet: function (data) { return data.updateDate.substring(0, 10) } },
                        { title: '操作', width: 120, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },
                    ]],
                    data: res.data
                });

                //分页
                laypage.render({
                    //挂载的分页id
                    elem: 'pagesbox',
                    count: res.totalCount,
                    limit: pageSize,
                    curr: pageIndex,
                    layout: ['count', 'prev', 'page', 'next', 'skip'],
                    jump: function (obj, first) {
                        if (!first) {
                            getListData(obj.curr);
                        }
                    }
                });
            }
        });
    }

    $('#searchBtn').on('click', function () {
        getListData(1);
    });

    //文档结构加载完之后执行
    $(function () {
        // 初始化加载数据
        getListData(1);
    })

    // 监听表单提交
    form.on('submit(save)', function (data) {
        var formData = {
            RoleId: 0,
            RoleName: $("#roleName").val(),
            RoleRemark: $("#roleRemark").val(),
            // NewEarTag: $("input[name='newEarTag']").val(),
            // Reason: $("#reasonsAdd").val(),

            CreateBy: "张三",
            CreateDate: Date.now,
        };

        $.ajax({
            url: 'http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Employee/AddRole', // 替换为您的后端API接口地址
            type: 'post',
            data: JSON.stringify(formData),
            contentType: 'application/json',
            success: function (res) {
                if (res > 0) {
                    alert("添加成功！");
                    location.href = '/Role/ShowRole'
                });
    } else {
        //layer.msg(res.msg, { icon: 2 });
        alert("添加失败！");
    }
            },
    error: function () {
        layer.msg('网络错误，请稍后重试', { icon: 2 });
    }
        });
return false; // 阻止表单跳转
    });

// 重置按钮点击事件
$('#closeBtn').on('click', function () {
    layer.closeAll();
});

// 单删除
function deleteData(roleId) {
    $.ajax({
        url: `http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Employee/FalseDelete?roleId=${roleId}`,
        type: 'delete',
        success: function (res) {
            if (res > 0) {
                layer.msg('删除成功');
                getListData(1);
            } else {
                layer.msg('删除失败');
                getListData(1);
            }
        },
        error: function () {
            layer.msg('删除请求失败');
        }
    });
}

// 监听工具条，实现删除功能
table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
    var data = obj.data;
    if (obj.event == 'edit') {
        editData(data.roleId); // 传递正确的数据项ID进行编辑操作
    }
    if (obj.event == 'delete') {
        layer.confirm('确定删除吗？', function (index) {
            deleteData(data.roleId); // 传递正确的数据项ID
            layer.close(index);
        });
    }
});

// 批量删除
$(".delAll_btn").click(function () {
    var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
        data = checkStatus.data,
        roleId = "";
    if (data.length > 0) {
        for (var i in data) {
            roleId += data[i].roleId + ",";
        }
        roleId = roleId.substring(0, roleId.length - 1);
        console.log(roleId);
        layer.confirm('确定删除选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
            $.ajax({
                url: `http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Employee/AllFalseDeleteRole?roleId=${roleId}`,
                type: 'put',
                success: function (res) {
                    if (res > 0) {
                        layer.msg("批量删除成功");
                        getListData(1);
                    } else {
                        layer.msg("批量删除失败");
                    }
                },
                error: function () {
                    layer.msg('批量删除请求失败');
                }
            });
            layer.close(index);
        });
    } else {
        layer.msg("请选择需要删除的记录");
    }
    console.log(checkStatus);
});

//反填
function editData(roleId) {
    $.ajax({
        url: 'http://10.31.56.6:8000/AuthorizationPeopleGetRout/api/RoleShow/BackRoleInfo?RoleId=' + roleId,
        type: 'get',
        contentType: 'application/json',
        success: function (res) {
            if (res != null) {
                // 填充编辑数据
                $("#roleName").val(res.roleName);
                $("#roleRemark").val(res.roleRemark);

                layer.open({
                    type: 1,
                    title: "修改新记录",
                    shift: 2,
                    area: ['500px', '300px'],
                    shade: 0,
                    shadeClose: false,
                    content: $("#add-main"),
                    success: function (layero, index) {
                        layui.form.render(); // 重新渲染表单
                    }
                });
            } else {
                layer.msg(res.msg, { icon: 5 });
            }
        },
        error: function (err) {
            // 处理请求错误
        }
    });
}
});

var $ = layui.jquery;
// 添加弹出层
function onAddBtn() {
    var laydate = layui.laydate;
    // 渲染
    laydate.render({
        elem: '#storageDate'
    });

    layer.open({
        type: 1,
        title: "添加角色信息",
        closeBtn: false,
        shift: 12,
        area: ['500px', '300px'],
        shade: 0,
        shadeClose: false,
        content: $("#add-main"),
        success: function (layero, index) {
        }
    });
}